package com.wisdudu.blank.ui.common.adapter;

import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wisdudu.blank.R;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.ui.login.GuideActivity;
import com.wisdudu.blank.ui.login.LoginActivity;


/**
 * Created by qyf on 2016/8/5.
 */
public class GuideAdapter extends PagerAdapter {

    private final TypedArray mGuideImgs;
    private final GuideActivity mContext;
    private final String mGuideFrom;

    public GuideAdapter(GuideActivity context, String guideFrom) {
        mContext = context;
        mGuideImgs = context.getResources().obtainTypedArray(R.array.guide_img);
        mGuideFrom = guideFrom;
    }

    @Override
    public int getCount() {
        return mGuideImgs.length();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_guide, container, false);
        ImageView ivGuideStart = (ImageView) view.findViewById(R.id.iv_guide_start);
        ImageView ivGuideBg = (ImageView) view.findViewById(R.id.iv_guide_bg);
        Glide.with(mContext).load(mGuideImgs.getResourceId(position,0)).into(ivGuideBg);
        if(position==getCount()-1){
            ivGuideStart.setVisibility(View.VISIBLE);
            ivGuideStart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!TextUtils.equals(mGuideFrom,"from_about_us")){
                        Intent intent = new Intent(mContext, LoginActivity.class);
                        UserRepo userRepo = Injection.provideUserRepo();
                        userRepo.setFirstLanuncher(false);
                        mContext.startActivity(intent);
                    }
                    mContext.finish();
                }
            });
        }else {
            ivGuideStart.setVisibility(View.GONE);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
